package com.ruoyi.report_ljlm.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.ruoyi.common.utils.BigDecimalUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.report_ljlm.domain.HdxywzxbgBsDetail;
import com.ruoyi.report_ljlm.domain.vo.HdxywzxbgBsDetailVo;
import com.ruoyi.report_ljlm.mapper.HdxywzxbgBsDetailMapper;
import com.ruoyi.syrw.annotation.ComputeReportResult;
import com.ruoyi.syrw.annotation.FIllBasicInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_ljlm.domain.bo.HdxywzxbgBsInfoBo;
import com.ruoyi.report_ljlm.domain.vo.HdxywzxbgBsInfoVo;
import com.ruoyi.report_ljlm.domain.HdxywzxbgBsInfo;
import com.ruoyi.report_ljlm.mapper.HdxywzxbgBsInfoMapper;
import com.ruoyi.report_ljlm.service.IHdxywzxbgBsInfoService;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 路面厚度、芯样完整性试验检测报告(钻芯法)Service业务层处理
 *
 * @author llb
 * @date 2024-07-10
 */
@RequiredArgsConstructor
@Service
public class HdxywzxbgBsInfoServiceImpl implements IHdxywzxbgBsInfoService {

    private final HdxywzxbgBsInfoMapper baseMapper;
    private final HdxywzxbgBsDetailMapper detailMapper;

    /**
     * 查询路面厚度、芯样完整性试验检测报告(钻芯法)
     */
    @Override
    @FIllBasicInfo
    public HdxywzxbgBsInfoVo queryById(Long excelId) {
        HdxywzxbgBsInfoVo hdxywzxbgBsInfoVo = baseMapper.selectVoById(excelId);
        List<HdxywzxbgBsDetailVo> detailList = detailMapper.selectVoList(
            new LambdaQueryWrapper<HdxywzxbgBsDetail>().eq(HdxywzxbgBsDetail::getExcelId, excelId));
        if (CollUtil.isNotEmpty(detailList)) {
            hdxywzxbgBsInfoVo.setDetailList(detailList);
        }
        return hdxywzxbgBsInfoVo;
    }

    /**
     * 查询路面厚度、芯样完整性试验检测报告(钻芯法)列表
     */
    @Override
    public TableDataInfo<HdxywzxbgBsInfoVo> queryPageList(HdxywzxbgBsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<HdxywzxbgBsInfo> lqw = buildQueryWrapper(bo);
        Page<HdxywzxbgBsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询路面厚度、芯样完整性试验检测报告(钻芯法)列表
     */
    @Override
    public List<HdxywzxbgBsInfoVo> queryList(HdxywzxbgBsInfoBo bo) {
        LambdaQueryWrapper<HdxywzxbgBsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<HdxywzxbgBsInfo> buildQueryWrapper(HdxywzxbgBsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HdxywzxbgBsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTotalPage() != null, HdxywzxbgBsInfo::getTotalPage, bo.getTotalPage());
        lqw.eq(bo.getPage() != null, HdxywzxbgBsInfo::getPage, bo.getPage());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancdwmc()), HdxywzxbgBsInfo::getJiancdwmc, bo.getJiancdwmc());
        lqw.eq(StringUtils.isNotBlank(bo.getBaogbh()), HdxywzxbgBsInfo::getBaogbh, bo.getBaogbh());
        lqw.eq(StringUtils.isNotBlank(bo.getShigwtdw()), HdxywzxbgBsInfo::getShigwtdw, bo.getShigwtdw());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcmc()), HdxywzxbgBsInfo::getGongcmc, bo.getGongcmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcbwyt()), HdxywzxbgBsInfo::getGongcbwyt, bo.getGongcbwyt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpmc()), HdxywzxbgBsInfo::getYangpmc, bo.getYangpmc());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpbh()), HdxywzxbgBsInfo::getYangpbh, bo.getYangpbh());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsl()), HdxywzxbgBsInfo::getYangpsl, bo.getYangpsl());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpzt()), HdxywzxbgBsInfo::getYangpzt, bo.getYangpzt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsj()), HdxywzxbgBsInfo::getYangpsj, bo.getYangpsj());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyj()), HdxywzxbgBsInfo::getJiancyj, bo.getJiancyj());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyj()), HdxywzxbgBsInfo::getPandyj, bo.getPandyj());
        lqw.eq(StringUtils.isNotBlank(bo.getZhuyyqsbmcjbh()), HdxywzxbgBsInfo::getZhuyyqsbmcjbh, bo.getZhuyyqsbmcjbh());
        lqw.eq(StringUtils.isNotBlank(bo.getWeitbh()), HdxywzxbgBsInfo::getWeitbh, bo.getWeitbh());
        lqw.eq(StringUtils.isNotBlank(bo.getJianclb()), HdxywzxbgBsInfo::getJianclb, bo.getJianclb());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancjl()), HdxywzxbgBsInfo::getJiancjl, bo.getJiancjl());
        lqw.eq(StringUtils.isNotBlank(bo.getFujsm()), HdxywzxbgBsInfo::getFujsm, bo.getFujsm());
        lqw.eq(StringUtils.isNotBlank(bo.getBgFlag()), HdxywzxbgBsInfo::getBgFlag, bo.getBgFlag());
        lqw.eq(StringUtils.isNotBlank(bo.getJianc()), HdxywzxbgBsInfo::getJianc, bo.getJianc());
        lqw.eq(StringUtils.isNotBlank(bo.getJil()), HdxywzxbgBsInfo::getJil, bo.getJil());
        lqw.eq(StringUtils.isNotBlank(bo.getFuh()), HdxywzxbgBsInfo::getFuh, bo.getFuh());
        lqw.eq(StringUtils.isNotBlank(bo.getRiq()), HdxywzxbgBsInfo::getRiq, bo.getRiq());
        lqw.eq(StringUtils.isNotBlank(bo.getShebIds()), HdxywzxbgBsInfo::getShebIds, bo.getShebIds());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyjIds()), HdxywzxbgBsInfo::getPandyjIds, bo.getPandyjIds());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyjIds()), HdxywzxbgBsInfo::getJiancyjIds, bo.getJiancyjIds());
        return lqw;
    }

    /**
     * 新增路面厚度、芯样完整性试验检测报告(钻芯法)
     */
    @Override
    @DSTransactional
    public Boolean insertByBo(HdxywzxbgBsInfoBo bo) {
        HdxywzxbgBsInfo add = BeanUtil.toBean(bo, HdxywzxbgBsInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
            ArrayList<HdxywzxbgBsDetail> details = new ArrayList<>(10);
            for (int i = 1; i <= 10; i++) {
                details.add(new HdxywzxbgBsDetail(bo.getExcelId()));
            }
            detailMapper.insertBatch(details);
        }
        return flag;
    }

    /**
     * 修改路面厚度、芯样完整性试验检测报告(钻芯法)
     */
    @Override
    @DSTransactional
    @ComputeReportResult
    public Boolean updateByBo(HdxywzxbgBsInfoBo bo) {
        // 增加对 是否合格 的填充
        fillExcelResult(bo);
        HdxywzxbgBsInfo update = BeanUtil.toBean(bo, HdxywzxbgBsInfo.class);
        validEntityBeforeSave(update);
        boolean flag = baseMapper.updateById(update) > 0;
        if (flag) {
            detailMapper.updateBatchById(BeanUtil.copyToList(bo.getDetailList(), HdxywzxbgBsDetail.class));
        }
        return flag;
    }

    private void fillExcelResult(HdxywzxbgBsInfoBo bo) {
        if (CollUtil.isNotEmpty(bo.getDetailList())) {
            BigDecimal add = new BigDecimal(0);
            int number = 0;
            for (int i = 0; i < bo.getDetailList().size(); i++) {
                if (!ObjectUtils.isEmpty(bo.getDetailList().get(i).getShichd())) {
                    add.add(bo.getDetailList().get(i).getShichd());
                    number = number + 1;
                }
            }
            if (number == 0) {
                bo.setExcelResult(1);
                return;
            }
            BigDecimal average = add.divide(new BigDecimal(number));
            if (!ObjectUtils.isEmpty(bo.getDetailList().get(0).getShejhd())) {
                if (BigDecimalUtil.isLessOrEqual(bo.getDetailList().get(0).getShejhd(), average)) {
                    bo.setExcelResult(2);
                    return;
                }
            }
            bo.setExcelResult(1);
        }
    }

    public boolean isWell(Long excelId) {
        return baseMapper.selectVoById(excelId).getExcelResult() != 2;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(HdxywzxbgBsInfo entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除路面厚度、芯样完整性试验检测报告(钻芯法)
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public HdxywzxbgBsInfoVo jcjl(HdxywzxbgBsInfoVo vo) {
        StringBuilder result = new StringBuilder("检测结论：经检测，所检指标符合《公路工程质量检验评定标准第一册土建工程》JTG F80/1-2017及设计文件要求。");
        vo.setJiancjl(result.toString());
        return vo;
    }
}
